package bb.lanxing.mvvm.base;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;

import bb.lanxing.activity.base.BaseActivity;

public abstract class BaseDBActivity<T extends ViewDataBinding> extends BaseActivity {
    protected T binding;

    public abstract int getLayoutId();

    public abstract void initView(T t);

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        binding = DataBindingUtil.setContentView(this, getLayoutId());
        binding.setLifecycleOwner(this);
        initView(binding);
    }

    public final ViewModel createViewModelFromScope(String scope, Class<? extends ViewModel> clazz) {
        ViewModelStoreOwner viewModelStoreByScope = VMStoreKt.getViewModelStoreByScope(this, scope);
        ViewModelProvider.Factory defaultViewModelProviderFactory = getDefaultViewModelProviderFactory();
        return new ViewModelProvider(viewModelStoreByScope, defaultViewModelProviderFactory).get(clazz);
    }
}
